<?php

namespace App\Http\Controllers\Admin\Config;

use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;

use App\Models\SmsTpl;
use App\Models\Config;

class SmsTplController extends AdminController
{
   	/**
     * 获取列表数据
     * @return [type] [description]
     */
    function getLists(Request $request){
        $pageIndex = (int)$request->input('page') ?  $request->input('page') : 1;
        $length    = (int)$request->input('length') ?  $request->input('length') : 10;
        $start     = ($pageIndex-1)*$length;

    	$where = [];
        if($config_id = $request->input('config_id')){
            $where['config_id'] = $config_id;
        }
        if($module = $request->input('module')){
            $where['module'] = $module;   
        }

        $smstplModel = SmsTpl::where($where);
        if($content=$request->input('content')){
            $smstplModel = $smstplModel->where('content', 'like', "%$content%");
        }

    	$data['total'] = $smstplModel->count();
    	if($data['total']>0){
    		$data['result'] = $smstplModel->orderBy('updated_at','desc')->skip($start)->take($length)->get()->toArray();
    	}
    	$this->ajaxJson(0,'',$data);
    }

    /**
     * 获取所有
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    function getAll(Request $request){
        $smstpls = SmsTpl::get()->toArray();
        $this->ajaxJson(0,'',$smstpls);
    }

    /**
     * 验证服务商下模块唯一
     * @return [type] [description]
     */
    function checkUniqueModule(Request $request){
        $config_id = $request->input('config_id');
        $module = $request->input('module');
        if($module=='' || $config_id==0){
            $this->ajaxJson(0,'',['count'=>0]);
        }
        $count = SmsTpl::where('id','!=',$request->input('id'))->where(['config_id' =>$config_id, 'module'=>$module])->count();
        $this->ajaxJson(0,'',['count'=>$count]);
    }

    /**
     * 获取单条数据
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function getOne($id){
        $smstpl = $this->_get_one($id);
        $this->ajaxJson(0,'',$smstpl);
    }

    /**
     * 更新一条数据
     * @return [type] [description]
     */
    function postOne(Request $request){
        $id = $request->input('id');
        if($id){
            $smstpl = $this->_get_one($id);
        }else{
            $smstpl = new SmsTpl();
        }
        $smstpl->content   = $request->input('content');
        $smstpl->config_id = $request->input('config_id');
        $smstpl->module    = $request->input('module');
        $smstpl->serviceId = $request->input('serviceId');
        $smstpl->save();
        $this->ajaxJson(0,'更新成功');
    }

    /**
     * 发送短信模版
     * @return [type] [description]
     */
    function sendTpl(Request $request){
        $smstpl = $this->_get_one($request->input('smstpl_id'));
        $config = Config::find($smstpl->config_id);
        if(!$config){
            $this->ajaxJson(1,'短信服务商已不存在,请添加服务商');
        }
        $serviceName = $config->vue;
        $phone = $request->input('phone');
        $url_params = $request->input('params');
        foreach ($url_params as $param) {
            $params[$param['key']] = $param['vue'];
        }
        $smstpl->sendSms($phone,$serviceName,$params,$request->getClientIp());
        $this->ajaxJson($smstpl['returnMsg']['code'],$smstpl['returnMsg']['msg']);
    }

     /**
     * 删除单条数据
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    function deleteOne($id){
        $smstpl = $this->_get_one($id);
        $smstpl->delete();
        $this->ajaxJson(0,'删除成功');
    }

    /**
     * 公共获取单条数据
     * @return [type] [description]
     */
    private function _get_one($id){
        $smstpl = SmsTpl::find($id);
        if(!$smstpl){
            $this->ajaxJson(1,'数据不存在,请刷新页面');
        }
        return $smstpl;
    }
}
